package com.example.campusforumapi.controller;

import com.example.campusforumapi.entity.*;
import com.example.campusforumapi.service.ArticleService;
import com.example.campusforumapi.util.AliOSSUtils;
import com.example.campusforumapi.util.PageResult;
import com.example.campusforumapi.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/articles")
public class ArticleController extends BaseController{

    @Autowired
    private ArticleService articleService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    // 允许的文档扩展名白名单
    private static final Set<String> ALLOWED_EXTENSIONS = new HashSet<>(Arrays.asList(
            "doc", "docx", "xls", "xlsx",
            "ppt", "pptx", "pdf", "txt",
            "csv", "rtf", "odt", "ods"
    ));

    //    查询所有文章标签记录
    @GetMapping("/getCategories")
    public Result getCategories() {
        List<Category> categoryList = articleService.getCategories();
        return Result.success(categoryList);
    }

    /*
     * 按条件  分页查询
     * */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
            , Integer articleId, String username, String title, Integer categoryId){

        //        查询并封装分页结果集
        PageResult articlePage = articleService.getArticlePage(page,pageSize,articleId,username,title,categoryId);

        return Result.success(articlePage);
    }

    //    删除一条或多条记录
    @DeleteMapping ("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        articleService.delArticle(ids);
        return Result.success();
    }

    //    新增一条记录
    @PostMapping ("/insert")
    public Result insert(@RequestBody Article article){
        if(StringUtils.isEmpty(article.getTitle()) || StringUtils.isEmpty(article.getUserId())){
            return Result.error("标题及作者id不能为空");
        }
        else if (StringUtils.isEmpty(article.getContent())) {
            return Result.error("文章内容不能为空");
        }
        else if(articleService.insertArticle(article) > 0){
            return Result.success("插入成功");
        }else{
            return Result.error("插入异常");
        }

    }

    //    修改一条记录
    @PutMapping("/update")
    public Result update(@RequestBody Article article){
        if(articleService.updArticle(article) > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败，该文章不存在");
        }

    }

    /*
     * 文章附件文件上传 获取阿里云url地址
     * */
    @PostMapping("/attachmentUpload")
    public Result attachmentUpload(@RequestParam("file") MultipartFile attachment) throws IOException {
        log.info("文件上传，文件名：{ }",attachment.getOriginalFilename());
        String url = aliOSSUtils.upload(attachment);
        return Result.success(url);

    }

    /*
     * 按条件  分页查询 审核状态的文章查询
     * */
    @RequestMapping(value = "/AuditList", method = RequestMethod.GET)
    public Result AuditList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
            , Integer articleId){

        //        查询并封装分页结果集
        PageResult auditArticlePage = articleService.getAuditArticlePage(page,pageSize,articleId);

        return Result.success(auditArticlePage);
    }
    //    活动审核通过时相应操作
    @PutMapping("/approve")
    public Result approve(@RequestBody Article article){
        log.info("{}",article.getArticleId());
        if(articleService.updAproveArticle(article) > 0){
            return Result.success("审核成功");
        }else{
            return Result.error("审核失败");
        }

    }
    //    活动审核通过时相应操作
    @PutMapping("/reject")
    public Result reject(@RequestBody Article article){
//        log.info("{}",article.getFeedback());
        if(articleService.updRejectArticle(article) > 0){
            return Result.success("操作成功");
        }else{
            return Result.error("操作失败");
        }

    }
}
